%% Parameters
% Lab-PC
% filename = 'D:\Server\FTP_server\Temp\origianl_maritx_01.fid\fid'
% filename = 'D:\Server\FTP_server\20130930_KSJ_CSI2D_test01\8by8M_20by50F_Water_01.fid\fid'
% filename = 'D:\Server\FTP_server\20131009_KSJ_NAA_CSI2D_RatBrainCoil01\origianl_maritx_volCoil_01.fid\fid'
% filename = 'D:\Server\FTP_server\20131015_KSJ_CSI2D_Gly_Cr_NAA_50mM_Phantom01\16by8M_35by20F_TR4s_2gain_Water_SatBand_01.fid\fid'
% filename = 'D:\Server\FTP_server\20131009_KSJ_NAA_CSI2D_RatBrainCoil01\origianl_maritx_volCoil_sqaure_01.fid\fid'
filename = 'D:\Server\FTP_server\20131015_KSJ_CSI2D_Gly_Cr_NAA_50mM_Phantom01\8by8M_16by16F_TR4s_2gain_Water_SatBand_01.fid\fid'
% filename = 'D:\Server\FTP_server\20131015_KSJ_CSI2D_Gly_Cr_NAA_50mM_Phantom01\8by8M_16by16F_TRmin_2gain_ZeroFlipAngle_NoSat_01.fid\fid'

% get header information.
cell_args = {3}; % default value; display nothing
[~, headerinfo] = readfidn(filename, 1, 1, cell_args);

nblocks=headerinfo{1, 1}(1)     % header info
ntraces=headerinfo{1, 1}(2)     % status info
dataP=headerinfo{1, 1}(3)/2;    % data points
nChannel = 4;

arr_data = zeros([dataP, nChannel]);

for indx = 1:1:nChannel
    arr_data(:, indx) = readfidn(filename, indx, 2, cell_args);
end

arr_phased_spectra = autoPhase(arr_data, {1, 5000});

figure;
plot(real(arr_phased_spectra(:,1)), 'red');
hold on;
plot(real(arr_phased_spectra(:,2)), 'blue');
plot(real(arr_phased_spectra(:,3)), 'cyan');
plot(real(arr_phased_spectra(:,4)), 'magenta');

av_data = arr_phased_spectra(:,1) + arr_phased_spectra(:,2) + arr_phased_spectra(:,3) + arr_phased_spectra(:,4);
av_data = av_data/4;

plot(real(av_data), 'black');


clear;